package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.statisVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {
        //目的地
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        Destination dest = strategy.getDest();
        detail.setDest(dest);
        //作者
        //摘要
        //获取摘要方案
        //1.攻略文章作者
        //2.截取，文章内容前200字符
        String content = detail.getStrategyContent().getContent();
        if (content.length() > 200){
            detail.setSummary(content.substring(0,200));
        }else {
            detail.setSummary(content);
        }
        //是否国外
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        detail.setIsabroad(toasts.get(0).getId() != 1L);//国外是true
        //内容
        StrategyContent strategyContent = detail.getStrategyContent();
        if (detail.getId() == null){
            //创建时间
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);
        }else {
            strategyDetailMapper.updateByPrimaryKey(detail);
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }
        //如果是编辑的话把当前明细的id删除了，再重新添加进对应的标签里面
        strategyDetailMapper.deleteRelation(detail.getId());
        //添加标签
        if (StringUtils.hasLength(tags)){
            //标签是独立的一个
            String[] ts = tags.split(",");
            //判断是否有数据
            if (ts != null && ts.length > 0){
                for (String t : ts) {
                    StrategyTag strategyTag = new StrategyTag();
                    strategyTag.setName(t);
                    //添加进去
                    strategyTagService.saveOrUpdate(strategyTag);
                    //添加进中间表维护数据
                    strategyDetailMapper.insertRelation(detail.getId(), strategyTag.getId());
                }
            }
        }
    }

    @Override
    public StrategyContent getContentById(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> getViewnumTop3(Long destId) {
        return strategyDetailMapper.getViewnumTop3(destId);
    }

    @Override
    public void updateStatisVo(statisVO vo) {
        strategyDetailMapper.updateStatisVo(vo);
    }
}
